/*-
 * Copyright (c) 2014 Shang-Rong Cai. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $ DIDF: did_optional.c    Rev. 0.0.3    2014-06-06T00:17+08:00 $
 */

#include <did.h>


int did_read_body_siwise(FILE *fpif, did_head_t *hdprt, did_meta_t *mtprt, void *dst, void *idx, void *val)
{
    int ierr = ERRNO_UNDEFINED;

    size_t idx_sz;
    size_t dat_sz;
    uint64_t ui, uj;  /* uint64_t based loop counter or index */

    void *buf_pos;
    void *buf_val;

#ifdef TRACE_INFO
    printf("[F] did_read_body_siwise()\n");
#endif

    /* Set Size By Index Type */
    ierr = get_index_type_size(hdprt->index_type, &idx_sz);

    if (ierr != ERRNO_UNDEFINED)
    {
        goto EXIT_DID_READ_BODY_SIWISE:
    }

    /* Set Size By Data Entry Type */
    ierr = get_entry_type_size(hdprt->entry_type, &dat_sz);

    if (ierr != ERRNO_UNDEFINED)
    {
        goto EXIT_DID_READ_BODY_SIWISE:
    }

    fseek(fpof, DID_ADDR_OFFSET_DATA + , SEEK_SET);

    /* First indices distribution depended on second index */
    itmz = fread(dst, idx_sz, (size_t) mtprt->snd_idx_sz + 1, fpif);

    /* First indices list */
    itmz = fread(idx, idx_sz, (size_t) mtprt->dat_ent_sz, fpif);

    /* Data entries list */
    buf_val = calloc(mtprt->dat_ent_sz, dat_sz);
    buf_pos = calloc(mtprt->dat_ent_sz, idx_sz);

    itmz = fread(buf_val, dat_sz, (size_t) mtprt->dat_ent_sz, fpif);
    switch (hdprt->index_type)
    {
        case DID_HEAD_INDEX_T_I32:
            for (ui = 0; ui < mtprt->snd_idx_sz; ui++)
            {
                for (uj = (uint64_t) *(dst + ui * idx_sz); uj < (uint64_t) *(dst + (ui + 1) * idx_sz); uj++)
                {
                    memcpy(val + uj * dat_sz,
                }
            }
            break;
        case DID_HEAD_INDEX_T_I64:
            for (ui = 0; ui < mtprt->snd_idx_sz; ui++)
            {
                for (uj = *(dst + ui * idx_sz); uj < *(dst + (ui + 1) *idx_sz); uj++)
                {

                }
            }
            break;
    }

/*
    for (ui = 0; ui < mtprt->dat_ent_sz; ui++)
    {
        fread(val + dat_sz * ui, dat_sz, 1, fpif);
    }

csr_mval[csr_rptr[csc_ridx[ii]] + csc_disp[ii]]
*/
    free(buf_val);
    free(buf_pos);

    EXIT_DID_READ_BODY_SIWISE:
#ifdef TRACE_INFO
    printf("[ ] did_read_body_siwise()\n");
#endif

    return ierr;
}
